package com.xuliugen.ssm.demo.facade.impl;

import com.xuliugen.ssm.demo.common.Response;
import com.xuliugen.ssm.demo.dto.SecurityUserDTO;
import com.xuliugen.ssm.demo.facade.SecurityUserFacade;
import com.xuliugen.ssm.demo.service.SecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * Created by xuliugen on 2017/5/2.
 */
@Service
public class SecurityUserFacadeImpl implements SecurityUserFacade {

    @Autowired
    private SecurityUserService userService;

    @Override
    public Response getUserById(Integer id) {
        Response response = new Response();

        try {
            SecurityUserDTO userDTOS = userService.getUserById(id);
            if (userDTOS != null) {
                response.setCode(HttpStatus.OK.value());
                response.setMsg("查询成功");
                response.setData(userDTOS);
            } else {
                response.setCode(HttpStatus.NOT_FOUND.value());
                response.setMsg("查询失败");
                response.setData(null);
            }
            return response;
        } catch (Exception e) {
            response = new Response(e);
        }
        return response;
    }

    @Override
    public Response addUser(SecurityUserDTO userDTO) {
        Response response = new Response();

        try {
            int flag = userService.addUser(userDTO);
            if (flag > 0) {
                response.setCode(HttpStatus.CREATED.value());
                response.setMsg("添加成功");
                response.setData(flag);
            } else {
                response.setCode(HttpStatus.UNAUTHORIZED.value());
                response.setMsg("添加失败");
                response.setData(null);
            }
            return response;
        } catch (Exception e) {
            response = new Response(e);
        }
        return response;
    }

    @Override
    public Response getUserByUserName(String userName) {
        Response response = new Response();

        try {
            int flag = userService.getUserCountByUserName(userName);
            if (flag == 0) {
                response.setCode(HttpStatus.OK.value());
                response.setMsg("用户不存在！可以添加！");
                response.setData(flag);
            } else {
                response.setCode(HttpStatus.CONFLICT.value());
                response.setMsg("用户已存在！不可以添加！");
                response.setData(null);
            }
            return response;
        } catch (Exception e) {
            response = new Response(e);
        }
        return response;
    }

    @Override
    public Response updateUser(SecurityUserDTO userDTO) {
        Response response = new Response();
        try {
            int flag = userService.updateUser(userDTO);
            if (flag > 0) {
                response.setCode(HttpStatus.OK.value());
                response.setMsg("修改用户成功！");
            } else {
                response.setCode(HttpStatus.UNAUTHORIZED.value());
                response.setMsg("修改用户失败！");
            }
        } catch (Exception e) {
            response = new Response(e);
        }
        return response;
    }

    @Override
    public Response delete(Integer userId) {
        Response response = new Response();

        try {
            int flag = userService.delete(userId);
            if (flag > 0) {
                response.setMsg("删除成功");
                response.setData(flag);
                response.setCode(HttpStatus.UNAUTHORIZED.value());
                response.setMsg("删除失败");
                response.setData(0);
            }
            response.setCode(HttpStatus.OK.value());
            return response;
        } catch (Exception e) {
            response = new Response(e);
        }
        return response;
    }

}
